﻿(function() {
    $(function() {
        var _releaseRecordService = abp.services.app.releaseRecord;

        var _$modal = $('#ReleaseModal');
        var _$form = _$modal.find('form');

        $('#RefreshButton').click(function () {
            refreshUserList();
        });

        
        //初始化分页
        $.paginationInit($('.pagination'), '', 7);

        function refreshUserList() {
            location.reload(true); //reload page to see new user!
        }

        $('.btn-feedback').click(function () {
            var releaserecordid = $(this).attr("data-feedback-releaserecordid");
            var environmentid = $(this).attr('data-feedback-environmentid');
            location.href = abp.appPath + 'ReleaseRecord/Feedbacks?ReleaseRecordId=' + releaserecordid + '&EnvironmentId=' + environmentid;
        });
        //反馈记录汇总
        $('.badge-feedbacktotal').each(function () {
            var $othis = $(this);
            var releaserecordid = $(this).attr("data-releaserecord-id");
            var environmentid = $(this).attr('data-releaserecord-environmentid');

            _releaseRecordService.getFeedbackStatics({ releaseRecordId: releaserecordid, environmentId: environmentid }).done(function (res) {
                var $_othis = $othis;
                $_othis.find('.badge-feedbacktotal-pending').text(res.pendingTotal);
                $_othis.find('.badge-feedbacktotal-claimed').text(res.claimedTotal);
                $_othis.find('.badge-feedbacktotal-processing').text(res.processingTotal);
                $_othis.find('.badge-feedbacktotal-processed').text(res.processedTotal);
            });

        });

        $('.btn-release').click(function (e) {
            var environmentid = $(this).attr('data-releaserecord-environmentid');
            var id = $(this).attr('data-releaserecord-id');
            _$form.find('input[name="ReleaseEnvironmentId"]').val(environmentid);
            _$form.find('input[name="ReleaseRecordId"]').val(id);
        });

        _$form.find('button[type="submit"]').click(function (e) {
            e.preventDefault();
            if (!_$form.valid()) {
                return;
            }

            var releaseRecord = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js

            abp.ui.setBusy(_$modal);
            _releaseRecordService.release(releaseRecord).done(function (res) {
                _$modal.modal('hide');
                console.log(res);
                location.reload(true); //reload page to see new user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });


        //查看日志详情
        $('.btn-showlog').click(function (e) {
            var releaserecordid = $(this).attr("data-feedback-releaserecordid");
            var environmentid = $(this).attr('data-feedback-environmentid');

            e.preventDefault();
            $.ajax({
                url: abp.appPath + 'ReleaseRecord/EditReleaseLogModal?ReleaseRecordId=' + releaserecordid + '&EnvironmentId=' + environmentid,
                type: 'POST',
                contentType: 'application/html',
                success: function (content) {
                    $('#ReleaseLogShowModal div.modal-content').html(content);
                },
                error: function (e) { }
            });
        });
    });
})();
